/***************************************************************************
   Filename:    Interface.cpp
   Author:      Josh Partridge
   Description: The interface class
****************************************************************************/

#include "Interface.h"

using namespace std;

/************************************************************************************
 Name:               Interface - default constructor
 Description:        empty
*************************************************************************************/
Interface::Interface()
{
}

/************************************************************************************
 Name:               readFile
 Description:        reads entire file as one string, line by line
 Input:
           filename: string representing name of file to read
 Output:
             return: string representing contents of file
*************************************************************************************/
string Interface::readFile(string filename)
{

    string msg = "";

    std::ifstream in(filename.c_str());

    string line;

    while(getline(in, line))
    {
        msg+=line + "\n";
    }
    in.close();
    in.clear();

    return msg;
}

/************************************************************************************
 Name:               splashTitle
 Description:        Displays the title splash
 Input:
              brush: reference to main's Brush
*************************************************************************************/
void Interface::splashTitle(Brush &brush)
{
    COORD original = brush.getPos();
    brush.clear();
    brush.move(0,0);
    cout << readFile("splash.txt");
    brush.move(original.X, original.Y);
}

/************************************************************************************
 Name:               dispScoreboard
 Description:        Displays the scoreboard ("top 10", "high scores", "hall of fame")
 Input:
              brush: reference to main's Brush
*************************************************************************************/
void Interface::dispScoreboard(Brush &brush)
{

    std::ifstream in ("highscores.txt");
        if(!in)
            return;

    string name;
    long double score;
    int place = 1;

    brush.clear();

    brush.setColor(cyan);
    brush.move(56, 1);
    cout << "HIGH SCORES";

    while(in>>name && place <= 10)
    {
        brush.move(45, 3 + (3*place));
        cout << place;
        brush.move(52, 3 + (3*place));
        cout << name;
        in >> score;
        brush.move(67, 3 + (3*place));
        cout << fixed << setprecision(0) << score;
        place++;
        brush.setColor(white);
    }

    brush.setColor(blue);
    // border function found in helpers
    singleLineBox(brush, 3, 2, 113, 35);

    brush.setColor(white);

    getch();
}

/************************************************************************************
 Name:               dispInstructions
 Description:        Displays the instructions file
 Input:
              brush: reference to main's Brush
*************************************************************************************/
void Interface::dispInstructions(Brush &brush)
{
    COORD original = brush.getPos();
    brush.clear();
    unsigned int scrolled = 0;

    std::ifstream in("instructions.txt");
    if(!in)
        return;

    vector<string> lines;
    string line;

    while(getline(in, line))
    {
        lines.push_back(line);
    }
    //the game crashes if it cannot fill the area with text
    while(lines.size()<32)
    {
        string line="\n";
        lines.push_back(line);
    }
    brush.setColor(blue);

    // border function found in helpers
    singleLineBox(brush, 3, 2, 113, 35);

    brush.setColor(cyan);

    brush.move(54, 1);
    cout << "INSTRUCTIONS";

    brush.move(50, 38);
    cout << "ESC to return to menu";

    if(lines.size() > maxSize)
    {
        brush.move(44,3);
        cout << "Use UP / DOWN keys to scroll";
    }

    brush.setColor(white);

    bool quit = false;
    bool changed = true;

    while (!quit)
    {
        if (changed)
        {
            for (unsigned int i = scrolled; i < scrolled + maxSize; i++)
            {
                brush.move( 6, i + 4 - scrolled);
                cout << lines.at( i ) << endl;
            }
        }
        changed = handleInput(getInput(), quit, lines.size(), scrolled);
        if(changed)
            brush.clearCenter();
    }

    brush.move(original.X, original.Y);
}

/************************************************************************************
 Name:               handleInput
 Description:        allows user to scroll and return to menu
 Input:
                 ch: character representing key pressed
               quit: reference to boolean representing if user wants to return to menu
             length: number of lines in the instructions file
           scrolled: reference to unsigned integer representing number of lines that
                     have been scrolled
*************************************************************************************/
bool Interface::handleInput(char ch, bool &quit, int length, unsigned int &scrolled)
{
    switch(ch)
    {
        case AR_UP:
            if(scrolled>0)
            {
                scrolled--;
                return true;
            }
            break;
        case AR_DOWN:
            if(scrolled<length-maxSize)
            {
                scrolled++;
                return true;
            }
            break;
        case 'x':
        case ESC: quit = true; break;
        default: break;
    }
    return false;
}
